home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr54 / bison118.zip / LEX.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  10KB  |  508 lines

  1. /* Token-reader for Bison's input parser,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* 
  22.    lex() is the entry point.  It is called from reader.c.
  23.    It returns one of the token-type codes defined in lex.h.
  24.    When an identifier is seen, the code IDENTIFIER is returned
  25.    and the name is looked up in the symbol table using symtab.c;
  26.    symval is set to a pointer to the entry found.  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "system.h"
  31. #include "files.h"
  32. #include "symtab.h"
  33. #include "lex.h"
  34. #include "new.h"
  35.  
  36.  
  37. extern int lineno;
  38. extern int translations;
  39.  
  40. int parse_percent_token();
  41.  
  42. extern void fatals();
  43. extern void fatal();
  44.  
  45. /* Buffer for storing the current token.  */
  46. char *token_buffer;
  47.  
  48. /* Allocated size of token_buffer, not including space for terminator.  */
  49. static int maxtoken;
  50.  
  51. bucket *symval;
  52. int numval;
  53.  
  54. static int unlexed;        /* these two describe a token to be reread */
  55. static bucket *unlexed_symval;    /* by the next call to lex */
  56.  
  57.  
  58. void
  59. init_lex()
  60. {
  61.   maxtoken = 100;
  62.   token_buffer = NEW2 (maxtoken + 1, char);
  63.   unlexed = -1;
  64. }
  65.  
  66.  
  67. static char *
  68. grow_token_buffer (p)
  69.      char *p;
  70. {
  71.   int offset = p - token_buffer;
  72.   maxtoken *= 2;
  73.   token_buffer = (char *) realloc(token_buffer, maxtoken + 1);
  74.   if (token_buffer == 0)
  75.     fatal("virtual memory exhausted");
  76.   return token_buffer + offset;
  77. }
  78.  
  79.  
  80. int
  81. skip_white_space()
  82. {
  83.   register int c;
  84.   register int inside;
  85.  
  86.   c = getc(finput);
  87.  
  88.   for (;;)
  89.     {
  90.       int cplus_comment;
  91.  
  92.       switch (c)
  93.     {
  94.     case '/':
  95.       c = getc(finput);
  96.       if (c != '*' && c != '/')
  97.         fatals("unexpected `/%c' found",c);
  98.       cplus_comment = (c == '/');
  99.  
  100.       c = getc(finput);
  101.  
  102.       inside = 1;
  103.       while (inside)
  104.         {
  105.           if (!cplus_comment && c == '*')
  106.         {
  107.           while (c == '*')
  108.             c = getc(finput);
  109.  
  110.           if (c == '/')
  111.             {
  112.               inside = 0;
  113.               c = getc(finput);
  114.             }
  115.         }
  116.           else if (c == '\n')
  117.         {
  118.           lineno++;
  119.           if (cplus_comment)
  120.             inside = 0;
  121.           else
  122.             c = getc(finput);
  123.         }
  124.           else if (c == EOF)
  125.         fatal("unterminated comment");
  126.           else
  127.         c = getc(finput);
  128.         }
  129.  
  130.       break;
  131.  
  132.     case '\n':
  133.       lineno++;
  134.  
  135.     case ' ':
  136.     case '\t':
  137.     case '\f':
  138.       c = getc(finput);
  139.       break;
  140.  
  141.     default:
  142.       return (c);
  143.     }
  144.     }
  145. }
  146.  
  147.  
  148. void
  149. unlex(token)
  150. int token;
  151. {
  152.   unlexed = token;
  153.   unlexed_symval = symval;
  154. }
  155.  
  156.  
  157.  
  158. int
  159. lex()
  160. {
  161.   register int c;
  162.   register char *p;
  163.  
  164.   if (unlexed >= 0)
  165.     {
  166.       symval = unlexed_symval;
  167.       c = unlexed;
  168.       unlexed = -1;
  169.       return (c);
  170.     }
  171.  
  172.   c = skip_white_space();
  173.  
  174.   switch (c)
  175.     {
  176.     case EOF:
  177.       return (ENDFILE);
  178.  
  179.     case 'A':  case 'B':  case 'C':  case 'D':  case 'E':
  180.     case 'F':  case 'G':  case 'H':  case 'I':  case 'J':
  181.     case 'K':  case 'L':  case 'M':  case 'N':  case 'O':
  182.     case 'P':  case 'Q':  case 'R':  case 'S':  case 'T':
  183.     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':
  184.     case 'Z':
  185.     case 'a':  case 'b':  case 'c':  case 'd':  case 'e':
  186.     case 'f':  case 'g':  case 'h':  case 'i':  case 'j':
  187.     case 'k':  case 'l':  case 'm':  case 'n':  case 'o':
  188.     case 'p':  case 'q':  case 'r':  case 's':  case 't':
  189.     case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
  190.     case 'z':
  191.     case '.':  case '_':
  192.       p = token_buffer;
  193.       while (isalnum(c) || c == '_' || c == '.')
  194.     {
  195.       if (p == token_buffer + maxtoken)
  196.         p = grow_token_buffer(p);
  197.  
  198.       *p++ = c;
  199.       c = getc(finput);
  200.     }
  201.  
  202.       *p = 0;
  203.       ungetc(c, finput);
  204.       symval = getsym(token_buffer);
  205.       return (IDENTIFIER);
  206.  
  207.     case '0':  case '1':  case '2':  case '3':  case '4':
  208.     case '5':  case '6':  case '7':  case '8':  case '9':
  209.       {
  210.     numval = 0;
  211.  
  212.     while (isdigit(c))
  213.       {
  214.         numval = numval*10 + c - '0';
  215.         c = getc(finput);
  216.       }
  217.     ungetc(c, finput);
  218.     return (NUMBER);
  219.       }
  220.  
  221.     case '\'':
  222.       translations = -1;
  223.  
  224.       /* parse the literal token and compute character code in  code  */
  225.  
  226.       c = getc(finput);
  227.       {
  228.     register int code = 0;
  229.  
  230.     if (c == '\\')
  231.       {
  232.         c = getc(finput);
  233.  
  234.         if (c <= '7' && c >= '0')
  235.           {
  236.         while (c <= '7' && c >= '0')
  237.           {
  238.             code = (code * 8) + (c - '0');
  239.             c = getc(finput);
  240.             if (code >= 256 || code < 0)
  241.               fatals("malformatted literal token `\\%03o'", code);
  242.           }
  243.           }
  244.         else
  245.           {
  246.         if (c == 't')
  247.           code = '\t';
  248.         else if (c == 'n')
  249.           code = '\n';
  250.         else if (c == 'a')
  251.           code = '\007';
  252.         else if (c == 'r')
  253.           code = '\r';
  254.         else if (c == 'f')
  255.           code = '\f';
  256.         else if (c == 'b')
  257.           code = '\b';
  258.         else if (c == 'v')
  259.           code = 013;
  260.         else if (c == 'x')
  261.           {
  262.             c = getc(finput);
  263.             while ((c <= '9' && c >= '0')
  264.                || (c >= 'a' && c <= 'z')
  265.                || (c >= 'A' && c <= 'Z'))
  266.               {
  267.             code *= 16;
  268.             if (c <= '9' && c >= '0')
  269.               code += c - '0';
  270.             else if (c >= 'a' && c <= 'z')
  271.               code += c - 'a' + 10;
  272.             else if (c >= 'A' && c <= 'Z')
  273.               code += c - 'A' + 10;
  274.             if (code >= 256 || code<0)/* JF this said if(c>=128) */
  275.               fatals("malformatted literal token `\\x%x'",code);
  276.             c = getc(finput);
  277.               }
  278.             ungetc(c, finput);
  279.           }
  280.         else if (c == '\\')
  281.           code = '\\';
  282.         else if (c == '\'')
  283.           code = '\'';
  284.         else if (c == '\"')    /* JF this is a good idea */
  285.           code = '\"';
  286.         else
  287.           {
  288.             if (c >= 040 && c <= 0177)
  289.               fatals ("unknown escape sequence `\\%c'", c);
  290.             else
  291.               fatals ("unknown escape sequence: `\\' followed by char code 0x%x", c);
  292.           }
  293.  
  294.         c = getc(finput);
  295.           }
  296.       }
  297.     else
  298.       {
  299.         code = c;
  300.         c = getc(finput);
  301.       }
  302.     if (c != '\'')
  303.       fatal("multicharacter literal tokens not supported");
  304.  
  305.     /* now fill token_buffer with the canonical name for this character
  306.        as a literal token.  Do not use what the user typed,
  307.        so that '\012' and '\n' can be interchangeable.  */
  308.  
  309.     p = token_buffer;
  310.     *p++ = '\'';
  311.     if (code == '\\')
  312.       {
  313.         *p++ = '\\';
  314.         *p++ = '\\';
  315.       }
  316.     else if (code == '\'')
  317.       {
  318.         *p++ = '\\';
  319.         *p++ = '\'';
  320.       }
  321.     else if (code >= 040 && code != 0177)
  322.       *p++ = code;
  323.     else if (code == '\t')
  324.       {
  325.         *p++ = '\\';
  326.         *p++ = 't';
  327.       }
  328.     else if (code == '\n')
  329.       {
  330.         *p++ = '\\';
  331.         *p++ = 'n';
  332.       }
  333.     else if (code == '\r')
  334.       {
  335.         *p++ = '\\';
  336.         *p++ = 'r';
  337.       }
  338.     else if (code == '\v')
  339.       {
  340.         *p++ = '\\';
  341.         *p++ = 'v';
  342.       }
  343.     else if (code == '\b')
  344.       {
  345.         *p++ = '\\';
  346.         *p++ = 'b';
  347.       }
  348.     else if (code == '\f')
  349.       {
  350.         *p++ = '\\';
  351.         *p++ = 'f';
  352.       }
  353.         else
  354.       {
  355.         *p++ = code / 0100 + '0';
  356.         *p++ = ((code / 010) & 07) + '0';
  357.         *p++ = (code & 07) + '0';
  358.       }
  359.     *p++ = '\'';
  360.     *p = 0;
  361.     symval = getsym(token_buffer);
  362.     symval->class = STOKEN;
  363.     if (! symval->user_token_number)
  364.       symval->user_token_number = code;
  365.     return (IDENTIFIER);
  366.       }
  367.  
  368.     case ',':
  369.       return (COMMA);
  370.  
  371.     case ':':
  372.       return (COLON);
  373.  
  374.     case ';':
  375.       return (SEMICOLON);
  376.  
  377.     case '|':
  378.       return (BAR);
  379.  
  380.     case '{':
  381.       return (LEFT_CURLY);
  382.  
  383.     case '=':
  384.       do
  385.     {
  386.       c = getc(finput);
  387.       if (c == '\n') lineno++;
  388.     }
  389.       while(c==' ' || c=='\n' || c=='\t');
  390.  
  391.       if (c == '{')
  392.           return(LEFT_CURLY);
  393.       else
  394.     {
  395.       ungetc(c, finput);
  396.       return(ILLEGAL);
  397.     }
  398.  
  399.     case '<':
  400.       p = token_buffer;
  401.       c = getc(finput);
  402.       while (c != '>')
  403.     {
  404.       if (c == '\n' || c == EOF)
  405.         fatal("unterminated type name");
  406.  
  407.       if (p == token_buffer + maxtoken)
  408.         p = grow_token_buffer(p);
  409.  
  410.       *p++ = c;
  411.       c = getc(finput);
  412.     }
  413.       *p = 0;
  414.       return (TYPENAME);
  415.         
  416.  
  417.     case '%':
  418.       return (parse_percent_token());
  419.  
  420.     default:
  421.       return (ILLEGAL);
  422.     }
  423. }
  424.  
  425.  
  426. /* parse a token which starts with %.  Assumes the % has already been read and discarded.  */
  427.  
  428. int
  429. parse_percent_token ()
  430. {
  431.   register int c;
  432.   register char *p;
  433.  
  434.   p = token_buffer;
  435.   c = getc(finput);
  436.  
  437.   switch (c)
  438.     {
  439.     case '%':
  440.       return (TWO_PERCENTS);
  441.  
  442.     case '{':
  443.       return (PERCENT_LEFT_CURLY);
  444.  
  445.     case '<':
  446.       return (LEFT);
  447.  
  448.     case '>':
  449.       return (RIGHT);
  450.  
  451.     case '2':
  452.       return (NONASSOC);
  453.  
  454.     case '0':
  455.       return (TOKEN);
  456.  
  457.     case '=':
  458.       return (PREC);
  459.     }
  460.   if (!isalpha(c))
  461.     return (ILLEGAL);
  462.  
  463.   while (isalpha(c) || c == '_')
  464.     {
  465.       if (p == token_buffer + maxtoken)
  466.     p = grow_token_buffer(p);
  467.  
  468.       *p++ = c;
  469.       c = getc(finput);
  470.     }
  471.  
  472.   ungetc(c, finput);
  473.  
  474.   *p = 0;
  475.  
  476.   if (strcmp(token_buffer, "token") == 0
  477.       ||
  478.       strcmp(token_buffer, "term") == 0)
  479.     return (TOKEN);
  480.   else if (strcmp(token_buffer, "nterm") == 0)
  481.     return (NTERM);
  482.   else if (strcmp(token_buffer, "type") == 0)
  483.     return (TYPE);
  484.   else if (strcmp(token_buffer, "guard") == 0)
  485.     return (GUARD);
  486.   else if (strcmp(token_buffer, "union") == 0)
  487.     return (UNION);
  488.   else if (strcmp(token_buffer, "expect") == 0)
  489.     return (EXPECT);
  490.   else if (strcmp(token_buffer, "start") == 0)
  491.     return (START);
  492.   else if (strcmp(token_buffer, "left") == 0)
  493.     return (LEFT);
  494.   else if (strcmp(token_buffer, "right") == 0)
  495.     return (RIGHT);
  496.   else if (strcmp(token_buffer, "nonassoc") == 0
  497.        ||
  498.        strcmp(token_buffer, "binary") == 0)
  499.     return (NONASSOC);
  500.   else if (strcmp(token_buffer, "semantic_parser") == 0)
  501.     return (SEMANTIC_PARSER);
  502.   else if (strcmp(token_buffer, "pure_parser") == 0)
  503.     return (PURE_PARSER);
  504.   else if (strcmp(token_buffer, "prec") == 0)
  505.     return (PREC);
  506.   else return (ILLEGAL);
  507. }
  508.